﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace SendMailLibray
{
    public class MailSender
    {
        MailMessage Message;
        SmtpClient _smtp;

        public MailSender()
        {
            this.Message = new MailMessage();
        }
        
        /// <summary>
        /// Mittente della mail
        /// </summary>
        public string From 
        {
            get
            {
                return this.Message.From.Address;
            }
            set
            {
                this.Message.From = new MailAddress(value);
            }
        }

        /// <summary>
        /// Lista degli indirizzi dei destinatari
        /// </summary>
        public MailAddressCollection To
        {
            get
            {
                return this.Message.To;
            }
        }

        /// <summary>
        /// Aggiunge un destinatario alla mail
        /// </summary>
        /// <param name="to">Indirizzo email da aggiungere</param>
        public void AddTo(string to)
        {
            this.Message.To.Add(new MailAddress(to));
        }

        /// <summary>
        /// Aggiunge un destinatario a copia per conoscenza
        /// </summary>
        /// <param name="cc"></param>
        public void AddCC(string cc)
        {
            this.Message.CC.Add(new MailAddress(cc));
        }

        public void AddBcc(string bcc)
        {
            this.Message.Bcc.Add(new MailAddress(bcc));
        }

        /// <summary>
        /// Testo principale della mail
        /// </summary>
        public string Text
        {
            get
            {
                return this.Message.Body;
            }
            set 
            { 
                this.Message.Body = value; 
            }
        }

        /// <summary>
        /// Determina se il testo della mail contiene html
        /// </summary>
        public bool IsHtmlBody 
        {
            get
            {
                return this.Message.IsBodyHtml;
            }
            set
            {
                this.Message.IsBodyHtml = value;
            }
        }

        /// <summary>
        /// Oggetto della mail
        /// </summary>
        public string Subject 
        {
            get
            {
                return this.Message.Subject;
            }
            set
            {
                this.Message.Subject = value;
            }
        }

        /// <summary>
        /// Indirizzo Server Smtp
        /// </summary>
        public string Smtp
        {
            get
            {
                return _smtp.Host ?? (string.Empty);
            }
            set
            {
                _smtp = new SmtpClient(value);
            }
        }

        /// <summary>
        /// Username server
        /// </summary>
        public string Username { get; set; }

        /// <summary>
        /// Password server
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Imposta l'utilizzo delle credenziali di default
        /// </summary>
        public bool UseDefaultCredentials  { get; set; }

        /// <summary>
        /// Imposta se il server richiede una connessione protetta
        /// </summary>
        public bool EnableSsl { get; set; }
        /// <summary>
        /// Invia la mail utilizzando le proprietà impostate
        /// </summary>
        public bool SendMail()
        {
            if (_smtp != null)
            {
                _smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                _smtp.EnableSsl = this.EnableSsl;
                if (UseDefaultCredentials)
                {
                    _smtp.UseDefaultCredentials = false;
                    if (!string.IsNullOrEmpty(this.Username) && !string.IsNullOrEmpty(this.Password))
                    {
                        _smtp.Credentials = new NetworkCredential(this.Username, this.Password);
                    }
                    else
                        throw new Exception("Username o Password non impostati");
                }
                else
                    _smtp.UseDefaultCredentials = true;

                if(this.Message.From == null)
                    throw new Exception("Mittente non impostato");
                else if(this.Message.To.Count <= 0)
                    throw new Exception("Nessun destinatario inserito");
                else if(string.IsNullOrEmpty(this.Message.Subject))
                    throw new Exception("Nessun oggetto inserito");
                else if(string.IsNullOrEmpty(this.Message.Body))
                    throw new Exception("Impossibile inviare una mail senza testo");
                else if (string.IsNullOrEmpty(this.Smtp))
                    throw new Exception("Non è stato impostato il server smpt");
                else
                {
                    try
                    {
                        this._smtp.Send(this.Message);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Impossibile inviare la mail");
                    }
                }
            }
            else
                return false;
        }
    }
}
